<template>
  <div>
    <el-table
        :data="shop"
        stripe
        style="width: 100%">
      <el-table-column
          prop="helpId"
          label="ID"
          width="180">
      </el-table-column>
      <el-table-column
          prop="goodsName"
          label="商品名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="date"
          label="活动时间"
          width="160px">
      </el-table-column>
      <el-table-column
          prop="helpNum"
          label="需助力数量"
          width="250px"
          align="center">
      </el-table-column>
      <el-table-column
              prop="helpTeamNum"
              label="已助力数量"
              width="250px"
              align="center">
      </el-table-column>
      <el-table-column
          label="操作">
        <template slot-scope="scope">
          <el-button @click="tagOpen(scope.row)" type="primary" size="mini" icon="el-icon-edit" circle></el-button>
          <el-button @click="deletes(scope.row)" type="danger" size="mini" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="助力修改" :visible.sync="tagFlag">
      <el-form :model="form">
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input
              v-model="form.name"
              :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间" :label-width="formLabelWidth">
          <el-date-picker
              v-model="form.date"
              type="datetime"
              placeholder="选择日期时间"
              default-time="12:00:00">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间" :label-width="formLabelWidth">
          <el-date-picker
                  v-model="form.day"
                  type="datetime"
                  placeholder="选择日期时间"
                  default-time="12:00:00">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="助力人数" :label-width="formLabelWidth">
          <el-input v-model="form.people" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存" :label-width="formLabelWidth">
          <el-input v-model="form.stock" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="tagFlag = false">取 消</el-button>
        <el-button type="primary" @click="updates">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "manage",
  inject:['reload'],
  data(){
    return{
      shop: [],
      formLabelWidth:'120px',
      form:{
        id:'',
        name:'',
        date:'',
        day:'',
        people:'',
        money:'',
        stock:'',
      },
      tagFlag:false,
    }
  },
  mounted() {
    this.loading();
  },
  methods:{
    tagOpen(row){
      this.form.id=row.helpId;
      this.form.name=row.goodsName;
      this.form.date=row.helpStartTime;
      this.form.day=row.helpEndTime;
      this.form.people=row.helpNum;
      this.form.money=row.goodsPrice;
      this.form.stock=row.helpStock;
      this.tagFlag=true;
    },
    updates(){
      this.$axios.post('/api/help/updateHelp',{helpId:this.form.id,helpStartTime:this.form.date,helpEndTime:this.form.day,helpNum:this.form.people,helpStock:this.form.stock}).then((res)=>{
        console.log(res.data)
        let updateMsg=res.data;
        if(updateMsg.length>0){
          if(updateMsg.code==200){
            this.$message({
              message: updateMsg.msg,
              type: 'success'
            });
            this.reload();
          }else if(updateMsg.code==1001){
            this.$message.error(updateMsg.msg);
          }
        }
      });
      this.tagFlag=false;
    },
    deletes(row){
      console.log(row)
      if(row.helpTeamNum<=row.helpNum){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post('/api/help/deleteHelp?helpId='+row.helpId).then((res)=>{
            if(res.data.code==200){
              this.$message({
                type: 'success',
                message: res.data.msg
              });
              this.reload();
            }else if(res.data.code==1001){
              this.$message.error(res.data.msg);
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }else {
        this.$message({
          type: 'info',
          message: '未助力完成的活动不能删除'
        });
      }
    },
    loading(){
      this.$axios.post('/api/help/getHelpList',{}).then((res)=>{
        let list=res.data.data;
        let arr=[];
        let brr=[];
        list.forEach((item)=>{
          arr.push(item.goods);
          if(item.helpTeam){
            brr.push(item.helpTeam);
          }
        });
        for(let i=0;i<list.length;i++){
          list[i].goodsName=arr[i].goodsName;
          if(brr[i]){
            list[i].helpTeamNum=brr[i].helpTeamNum;
          }else {
            list[i].helpTeamNum=0;
          }
        }
        list.forEach((item,index)=>{
          this.shop.push(item);
          this.shop[index].date=item.helpStartTime+' '+item.helpEndTime;
        });
      })
    },
  }
}
</script>

<style scoped>

</style>